<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="店铺">
                <a-auto-complete
                  v-model="queryParam.storeName"
                  :data-source="productSnDataSource"
                  @select="onSelect"
                  placeholder="请选择门店"
                  :filter-option="filterOption"
                />
            </a-form-item>

          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="要货单号">
              <a-input placeholder="请输入要货单号" v-model="queryParam.id"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="状态">
              <!-- <j-multi-select-tag v-model="status_MultiString" placeholder="请选择状态" dictCode="store_purchasing_status"/>-->
              <!--不使用数据字典了-->
              <j-multi-select-tag v-model="status_MultiString" placeholder="请选择状态" :options="jselectMultiple.options"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handlePrint" icon="printer">打印</a-button>
      <a-button icon="download" @click="handleExportXls('门店要货单')">导出</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>
        <span>已选择</span>
        <a style="font-weight: 600">
          {{ selectedRowKeys.length }}
        </a>
        <span>项</span>
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange,type:type}"
        @change="handleTableChange"
        :customRow="clickThenCheck">

        <span slot="action" slot-scope="text, record" v-if="record.status == 1||record.status == 3">
          <a v-if="record.status == 1" @click="goOA()">推送到OA</a>
          <a v-if="record.status == 3" @click="handleRollback(record.id,'3')">仓库已发货</a>

          <a-divider type="vertical" />
          <a @click="handleRollback(record.id,'7')">作废</a>
        </span>
        <span v-else>-</span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="门店要货明细" :key="refKeys[0]" :forceRender="true">
        <stmRequiregoodsItem-List ref="StmRequiregoodsItemList"></stmRequiregoodsItem-List>

      </a-tab-pane>
    </a-tabs>

    <!-- 表单区域 -->
    <stmReplenishment-modal ref="modalForm" @ok="modalFormOk"/>
    <StmRequiregoods-modal ref="modalOAForm" @ok="modalFormOk"/>
    <stm-replenishment-print-modal ref="printForm" @ok="modalFormOk"/>
    <stm-replenishment-exec-modal ref="execForm" @ok="modalFormOk"/>

  </a-card>
</template>

<script>
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import StmReplenishmentModal from './modules/StmReplenishmentModal'
  import StmRequiregoodsModal from './modules/StmRequiregoodsModal'
  import StmReplenishmentExecModal from './modules/StmReplenishmentExecModal'
  import JMultiSelectTag from '../../components/dict/JMultiSelectTag'
  import {deleteAction} from '@/api/manage'
  import { httpAction, putAction,getAction, postAction } from '@/api/manage'
  import StmRequiregoodsItemList from './StmRequiregoodsItemList'
  import StmReplenishmentItemModal from './modules/StmReplenishmentItemModal'
  import StmReplenishmentPrintModal from './modules/StmReplenishmentPrintModal'

  export default {
    name: "StmReplenishmentList",
    mixins: [JeecgListMixin],
    components: {
      StmReplenishmentModal,
      StmRequiregoodsModal,
      StmReplenishmentExecModal,
      StmReplenishmentItemModal,
      // StmRequiregoodsItemList,
      StmRequiregoodsItemList,
      StmReplenishmentPrintModal,
      JMultiSelectTag,
    },
    data () {
      return {
        jselectMultiple: {
          options: [
            { text: '待推送', value: '1' },
            { text: '待发货', value: '3' },
            { text: '待收货', value: '5' },
            { text: '已完成', value: '7' },
            { text: '已取消', value: '4' }
          ],
          value: 'Integer,Boolean'
        },
        refKeys: ['StmRequiregoodsItem' ],
        description: '门店要货单管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: "center",
            customRender:function (t, r, index) {
              return parseInt(index)+1;
            }
          },
          {
            title: '要货单号',
            align:"center",
            dataIndex: 'id'
          },
          {
            title: '店铺',
            align:"center",
            dataIndex: 'storeId_dictText'
          },
          // status_dictText
          {
            title: '状态',
            align:"center",
            dataIndex: 'status_dictText'
          },
          {
            title: '申请人',
            align:"center",
            dataIndex: 'createSales'
          },
          {
            title: '申请时间',
            align:"center",
            dataIndex: 'createTime'
          },
          {
            title: '审核人',
            align:"center",
            dataIndex: 'status',
            customRender:(text,record)=>{
              if (text == '4'){
                return record.cancelSales
              }else{
                return record.confirmSales
              }
            }
          },
          {
            title: '审核时间',
            align:"center",
            dataIndex: 'status',
            customRender:(text,record)=>{
              if (text == '4'){
                return record.cancelTime
              }else{
                return record.confirmTime
              }
            }
          },
          {
            title: '操作',
            width: 250,
            dataIndex: 'action',
            align:"center",
            scopedSlots: { customRender: 'action' },
          }
        ],

        //操作url
        type: "radio",
        url: {
          storeIdList:"/stm/stmStoreInfo/list",
          queryList: "/pms/pmsProduct/queryList",
          list: "/stm/stmReplenishmentV2/list",
          delete: "/stm/stmReplenishment/delete",
          deleteBatch: "/stm/stmReplenishment/deleteBatch",
          exportXlsUrl: "stm/stmReplenishment/exportXls",
          importExcelUrl: "stm/stmReplenishment/importExcel",
          generateInout: "/stm/stmReplenishment/generateInout",
          generateTransfer: "/stm/stmReplenishment/generateTransfer",
          applyUrl: "/stm/stmReplenishmentV2/apply",
          sourceList:"/oa/source/list" //库存地点
        },
        status_MultiString: '',
        productSnDataSource:[],
        dataResult:[],
        sourceResult:[]
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    created() {
      this.queryParam.status_MultiString = ''
      getAction(this.url.storeIdList,{ pageNo: 1, pageSize:100 }).then(res => {
        console.log('678');
        console.log(res);
        if(res.success){
          this.dataResult = res.result.records;
          this.productSnDataSource =  [... new Set(res.result.records.map(data => (data['name'])))];
        }else{
          console.log(res.message);
        }
      })
      this.handleSourceList()
    },
    watch: {
      status_MultiString(v) {
        this.queryParam.status_MultiString = this.status_MultiString
      }
    },
    methods: {
      // searchReset() {
      //   console.log('2323232');
      //   this.queryParam.status_MultiString = ''
      //   this.loadData(1);
      //   console.log(this.queryParam);
      // },
      onSelect(value){
        console.log("aaaa");
        console.log(value);
        this.storeName = value
        console.log(this.dataResult);
        this.queryParam.storeId = this.dataResult.filter(i => i.name == this.storeName)[0].storeId
        console.log(this.queryParam.storeId);
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLocaleLowerCase().indexOf(input.toLocaleLowerCase()) >= 0
        );
    },
		  clickThenCheck(record) {
        return {
          on: {
            click: () => {
              this.onSelectChange(record.id.toString().split(","), [record]);
            }
          }
        };
      },
      onSelectChange(selectedRowKeys, selectionRows) {
        this.selectedRowKeys = selectedRowKeys;
        this.selectionRows = selectionRows;
        this.$refs.StmRequiregoodsItemList.getMain(this.selectedRowKeys.toString());
      },
      onClearSelected() {
        this.selectedRowKeys = [];
        this.selectionRows = [];
        this.$refs.StmRequiregoodsItemList.queryParam.mainId = null;
        this.$refs.StmRequiregoodsItemList.loadData();
        this.$refs.StmRequiregoodsItemList.selectedRowKeys = [];
        this.$refs.StmRequiregoodsItemList.selectionRows = [];
      },

      handleDelete: function (id) {
        var that = this;
        deleteAction(that.url.delete, {id: id}).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
            this.$refs.StmRequiregoodsItemList.loadData();
          } else {
            that.$message.warning(res.message);
          }
        });
      },
      handleSourceList:function(){
        var that = this;
        getAction(that.url.sourceList).then((res) => {
          if (res.success) {
            // that.$message.success(res.message);
            this.sourceResult = res
          } else {
            that.$message.warning(res.message);
          }
        });
      },
      searchQuery:function(){
        this.selectedRowKeys = [];
        this.selectionRows = [];
        this.$refs.StmRequiregoodsItemList.queryParam.mainId = null;
        this.$refs.StmRequiregoodsItemList.loadData();
        this.$refs.StmRequiregoodsItemList.selectedRowKeys = [];
        this.$refs.StmRequiregoodsItemList.selectionRows = [];
        if(!this.queryParam.storeName || this.queryParam.storeName === 0){
          this.queryParam.storeId = ""
        };
        this.loadData();
      },
      handlePrint: function() {
        this.$refs.printForm.show(this.selectedRowKeys.toString());
        this.$refs.printForm.title = "打印";
      },

      handleRollback: function (id,tag) {
        if (!id && this.selectedRowKeys.length <= 0) {
          this.$message.warning('请选择一条记录！');
          return;
        } else {
          let params = {};
          params.status = tag;
          params.ids = [];

          if (id) {
            params.ids.push(id);
          }
          let that = this;
          const value = tag=='3'?'仓库已发货':'作废'
          this.$confirm({
            title: '是否确认'+ value+'？',
            onOk: function () {
              that.loading = true;
              // 调用同意和拒绝的接口
              postAction(that.url.applyUrl, params).then((res) => {
                if (res.success) {
                  that.$message.success(res.message);
                  that.loadData();
                  that.onClearSelected();
                } else {
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.loading = false;
              });

            }
          });
        }
      },

      goOA:function(){
        this.$refs.modalOAForm.add(this.selectionRows[0].id)
        this.$refs.modalOAForm.handleSource(this.sourceResult,'YH')
        this.$refs.modalOAForm.title = '推送到OA-要货'
      }

    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>
